package com.jade.editor.domain.parts;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
//import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;


public class SettingsRuler extends Dialog {

	private boolean _vertTrack = false;
	private boolean _horzTrack = false;
	private int _bigStep = 50;
	private int _lilStep = 5;
	private int _metric = DrawArea.METRIC_PX;
//	private int _ppi= 96;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsRuler(Shell parentShell) {
		super(parentShell);
	}
	
	public SettingsRuler(Shell shl, int bigStep, int lilStep, int metric, boolean vertTrack, boolean horzTrack){
		super(shl);
		_vertTrack = vertTrack;
		_horzTrack = horzTrack;
		_bigStep = bigStep;
		_lilStep = lilStep;
		_metric = metric;
//		s_ppi = getPPI(shl);
		
//		if (_metric == DrawArea.METRIC_CM){
//			double cmInpx = 2.56/_ppi; //mm in one px
//			double pxTomm = cmInpx * _bigStep;
//			double vol = Math.rint(100.0 * pxTomm)/100.0;
//			_bigStep = (int) (vol*100.0);
//			
//			pxTomm = cmInpx * _lilStep;
//			vol = Math.rint(100.0 * pxTomm)/100.0;
//			_lilStep = (int) (vol*100.0);
//		}
//		else if (_metric == DrawArea.METRIC_MM){
//			double mmInpx = 25.6/_ppi; //mm in one px
//			double pxTomm = mmInpx * _bigStep;
//			double vol = Math.rint(100.0 * pxTomm)/100.0;
//			_bigStep = (int) (vol*100.0);
//			
//			pxTomm = mmInpx * _lilStep;
//			vol = Math.rint(100.0 * pxTomm)/100.0;
//			_lilStep = (int) (vol*100.0);
//		}
	}
	
	public boolean getVerticalTrack(){
		return _vertTrack;
	}
	
	public boolean getHorizontalTrack(){
		return _horzTrack;
	}
	
	public int getBigStep(){
		return _bigStep;
	}
	
	public int getLilStep(){
		return _lilStep;
	}
	
	public int getMertic(){
		return _metric;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		final Spinner bigSpinner = new Spinner(container, SWT.BORDER);
		bigSpinner.setBounds(71, 15, 74, 22);
		
		final Spinner lilSpinner = new Spinner(container, SWT.BORDER);
		lilSpinner.setBounds(71, 59, 74, 22);
		bigSpinner.setDigits(0);
		lilSpinner.setDigits(0);
		bigSpinner.setSelection(_bigStep);
		lilSpinner.setSelection(_lilStep);
		bigSpinner.setMinimum(5);
		lilSpinner.setMinimum(1);
		bigSpinner.setMaximum(500);
		lilSpinner.setMaximum(_bigStep);
		
//		if (_metric == DrawArea.METRIC_PX){
//			bigSpinner.setDigits(0);
//			lilSpinner.setDigits(0);
//			bigSpinner.setSelection(_bigStep);
//			lilSpinner.setSelection(_lilStep);
//			bigSpinner.setMinimum(5);
//			lilSpinner.setMinimum(1);
//			bigSpinner.setMaximum(500);
//			lilSpinner.setMaximum(_bigStep);
//		}
//		else if (_metric == DrawArea.METRIC_MM){
//			double mmInpx = 25.6/_ppi; //mm in one px
//			bigSpinner.setDigits(2);
//			lilSpinner.setDigits(2);
//			bigSpinner.setSelection(_bigStep);
//			lilSpinner.setSelection(_lilStep);
//			bigSpinner.setMinimum((int)(mmInpx * 5 * 100));
//			bigSpinner.setMaximum((int)(mmInpx * 500 * 100));
//			
//			lilSpinner.setMinimum((int)(mmInpx * 1 * 100)+1);
//			lilSpinner.setMaximum(bigSpinner.getSelection());
//		}
//		else if (_metric == DrawArea.METRIC_CM){
//			double cmInpx = 2.56/_ppi; //mm in one px
//			bigSpinner.setDigits(2);
//			lilSpinner.setDigits(2);
//			bigSpinner.setSelection(_bigStep);
//			lilSpinner.setSelection(_lilStep);
//			bigSpinner.setMinimum((int)(cmInpx * 5 * 100));
//			bigSpinner.setMaximum((int)(cmInpx * 500 * 100));
//			
//			lilSpinner.setMinimum((int)(cmInpx * 1 * 100)+1);
//			lilSpinner.setMaximum(bigSpinner.getSelection());
//		}
		
		bigSpinner.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				lilSpinner.setMaximum(bigSpinner.getSelection());
				_bigStep = bigSpinner.getSelection();
				_lilStep = lilSpinner.getSelection();
			}
		});
		
		lilSpinner.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				_lilStep = lilSpinner.getSelection();
			}
		});
		
		
		final Combo combo = new Combo(container, SWT.NONE);
		combo.setBounds(169, 35, 152, 23);
		combo.add("Pixels");
//		combo.add("Millimeters");
		combo.add("Centimeters");
		if(_metric == DrawArea.METRIC_PX)
			combo.select(0);
		else if (_metric == DrawArea.METRIC_CM){
			combo.select(1);
			bigSpinner.setEnabled(false);
			lilSpinner.setEnabled(false);
			}
		
		combo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
//				double mmInpx = 25.6/_ppi; //mm in one px
//				double cmInpx = 2.56/_ppi; //cm in one px
//				double pxInmm = _ppi/25.6;	//px in one mm
//				double pxIncm = _ppi/2.56;	//px in one mm
//				
//				if(_metric == DrawArea.METRIC_PX && combo.getSelectionIndex() == DrawArea.METRIC_MM){	//px to mm
//					int valueBig = bigSpinner.getSelection();
//					int valueLil = lilSpinner.getSelection();
//					
//					bigSpinner.setMinimum((int)(mmInpx * 5 * 100));
//					bigSpinner.setMaximum((int)(mmInpx * 500 * 100));
//					
//					lilSpinner.setMinimum((int)(mmInpx * 1 * 100)+1);
//					lilSpinner.setMaximum(9999999);
//					
//					bigSpinner.setDigits(2);
//					lilSpinner.setDigits(2);
//					
//					double pxTommBig = mmInpx * valueBig;
//					double pxTommLil = mmInpx * valueLil;
//					
//					double volBig = Math.rint(100.0 * pxTommBig)/100.0;
//					double volLil = Math.rint(100.0 * pxTommLil)/100.0;
//					
//					int resBig = (int) (volBig*100.0);
//					int resLil = (int) (volLil*100.0);
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					_metric = DrawArea.METRIC_MM;
//				}
//				else if(_metric == DrawArea.METRIC_PX && combo.getSelectionIndex() == DrawArea.METRIC_CM){	//px to cm
//					
//					int valueBig = bigSpinner.getSelection();
//					int valueLil = lilSpinner.getSelection();
//					
//					bigSpinner.setMinimum((int)(cmInpx * 5 * 100));
//					bigSpinner.setMaximum((int)(cmInpx * 500 * 100));
//					
//					lilSpinner.setMinimum((int)(cmInpx * 1 * 100)+1);
//					lilSpinner.setMaximum(9999999);
//					
//					bigSpinner.setDigits(2);
//					lilSpinner.setDigits(2);
//					
//					double pxTocmBig = cmInpx * valueBig;
//					double pxTocmLil = cmInpx * valueLil;
//					
//					double volBig = Math.rint(100.0 * pxTocmBig)/100.0;
//					double volLil = Math.rint(100.0 * pxTocmLil)/100.0;
//					
//					int resBig = (int) (volBig*100.0);
//					int resLil = (int) (volLil*100.0);
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					_metric = DrawArea.METRIC_CM;
//				}
//				else if(_metric == DrawArea.METRIC_MM && combo.getSelectionIndex() == DrawArea.METRIC_PX){	//mm to px
//					
//					double valueBig = bigSpinner.getSelection() / 100.0;
//					double valueLil = lilSpinner.getSelection() / 100.0;
//					
//					bigSpinner.setDigits(0);
//					lilSpinner.setDigits(0);
//					bigSpinner.setMinimum(5);
//					lilSpinner.setMinimum(1);
//					bigSpinner.setMaximum(500);
//					lilSpinner.setMaximum(5);
//					
//					int resBig = (int) (pxInmm * valueBig);
//					int resLil = (int) (pxInmm * valueLil);
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					
//					_metric = DrawArea.METRIC_PX;
//				}
//				else if(_metric == DrawArea.METRIC_MM && combo.getSelectionIndex() == DrawArea.METRIC_CM){	//mm to cm
//					
//					double valueBig = bigSpinner.getSelection() / 10.0;
//					double valueLil = lilSpinner.getSelection() / 10.0;
//					
//					bigSpinner.setMinimum((int)(cmInpx * 5 * 100));
//					bigSpinner.setMaximum((int)(cmInpx * 500 * 100));
//					
//					lilSpinner.setMinimum((int)(cmInpx * 1 * 100)+1);
//					lilSpinner.setMaximum(9999999);
//					
//					int resBig = (int) valueBig;
//					int resLil = (int) valueLil;
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					_metric = DrawArea.METRIC_CM;
//				}
//				else if(_metric == DrawArea.METRIC_CM && combo.getSelectionIndex() == DrawArea.METRIC_PX){	//cm to px
//					
//					double valueBig = bigSpinner.getSelection() / 100.0;
//					double valueLil = lilSpinner.getSelection() / 100.0;
//					
//					bigSpinner.setDigits(0);
//					lilSpinner.setDigits(0);
//					bigSpinner.setMinimum(5);
//					lilSpinner.setMinimum(1);
//					bigSpinner.setMaximum(500);
//					lilSpinner.setMaximum(5);
//					
//					int resBig = (int) (pxIncm * valueBig);
//					int resLil = (int) (pxIncm * valueLil);
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					_metric = DrawArea.METRIC_PX;
//				}
//				else if(_metric == DrawArea.METRIC_CM && combo.getSelectionIndex() == DrawArea.METRIC_MM){	//cm to mm
//					
//					double valueBig = bigSpinner.getSelection() * 10.0;
//					double valueLil = lilSpinner.getSelection() * 10.0;
//					
//					bigSpinner.setMinimum((int)(cmInpx * 5 * 100));
//					bigSpinner.setMaximum((int)(cmInpx * 500 * 100));
//					
//					lilSpinner.setMinimum((int)(cmInpx * 1 * 100)+1);
//					lilSpinner.setMaximum(9999999);
//					
//					int resBig = (int) valueBig;
//					int resLil = (int) valueLil;
//					
//					bigSpinner.setSelection(resBig);
//					lilSpinner.setSelection(resLil);
//					
//					lilSpinner.setMaximum(bigSpinner.getSelection());
//					
//					_metric = DrawArea.METRIC_MM;
//				}
//				
				_lilStep = lilSpinner.getSelection();
				_bigStep = bigSpinner.getSelection();
				
				if (combo.getSelectionIndex() == 1) {
					_metric = DrawArea.METRIC_CM;
					bigSpinner.setEnabled(false);
					lilSpinner.setEnabled(false);
				}
				else if (combo.getSelectionIndex() == 0) {
					_metric = DrawArea.METRIC_PX;
					bigSpinner.setEnabled(true);
					lilSpinner.setEnabled(true);
				}
				
				
			}
			
		});
		
		Button vertCheckButton = new Button(container, SWT.CHECK);
		vertCheckButton.setBounds(183, 83, 138, 16);
		vertCheckButton.setText("Vertical mouse track");
		vertCheckButton.setSelection(_vertTrack);
		vertCheckButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				_vertTrack = _vertTrack ? false : true;
			}
		});
		
		Button horCheckButton = new Button(container, SWT.CHECK);
		horCheckButton.setBounds(183, 105, 152, 16);
		horCheckButton.setText("Horizontal mouse track");
		horCheckButton.setSelection(_horzTrack);
		horCheckButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				_horzTrack = _horzTrack ? false : true;
			}
		});
		
		Label bigStepLabel = new Label(container, SWT.NONE);
		bigStepLabel.setBounds(10, 18, 55, 15);
		bigStepLabel.setText("Big step:");
		
		Label lilStepLabel = new Label(container, SWT.NONE);
		lilStepLabel.setBounds(10, 62, 55, 15);
		lilStepLabel.setText("Little Step:");

		return container;
	}

	
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Ruler Settings");
	}
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
//				if (_metric == DrawArea.METRIC_MM){	//mm to px
//					double valueBig = _bigStep / 100.0;
//					double valueLil = _lilStep / 100.0;
//					double pxInmm = _ppi/25.6;
//					_bigStep = (int) (pxInmm * valueBig);
//					_lilStep = (int) (pxInmm * valueLil);
//				}
//				else if (_metric == DrawArea.METRIC_CM){	//cm to px
//					double valueBig = _bigStep / 100.0;
//					double valueLil = _lilStep / 100.0;
//					double pxInmm = _ppi/2.56;
//					_bigStep = (int) (pxInmm * valueBig);
//					_lilStep = (int) (pxInmm * valueLil);
//				}
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(352, 216);
	}
	
//	private int getPPI(Shell shell){
//		Device device = shell.getDisplay();
//		return device.getDPI().x;
//	}
	

}
